package com.his.outpatientvisit.service.impl;

import com.his.login.entity.DictEmployee;
import com.his.outpatientvisit.dao.PatiOutVisitSettleMapper;
import com.his.outpatientvisit.entity.PatiOutVisitSettle;
import com.his.outpatientvisit.service.PatiOutVisitSettleService;
import com.his.outpatientvisit.vo.PatiOutVisitSettleVo;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.WorkIdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PatiOutVisitSettleServiceImpl implements PatiOutVisitSettleService {

    @Resource
    private PatiOutVisitSettleMapper patiOutVisitSettleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertPatiOutVisitSettle(DictEmployee employee,PatiOutVisitSettleVo patiOutVisitSettleVo,String serialNumber)  throws Exception{
        PatiOutVisitSettle patiOutVisitSettle = BeanUtil.copyBean(patiOutVisitSettleVo,PatiOutVisitSettle.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date today= new Date();

        //结算号
        String settleID = WorkIdUtil.nextId();
        //结算日期
        Date settleDate = simpleDateFormat.parse(simpleDateFormat.format(today));
        //新增日期
        Date insertdate = settleDate;
        //更新日期
        Date updatedate = settleDate;
        //医院id
        String hospid  = employee.getHOSPID();
        //操作员
        String empid = employee.getEMPID();


        patiOutVisitSettle.setSettleid(settleID);
        patiOutVisitSettle.setSettledate(settleDate);
        patiOutVisitSettle.setCreatetime(insertdate);
        patiOutVisitSettle.setUpdatetime(updatedate);
        patiOutVisitSettle.setEmpid(empid);
        patiOutVisitSettle.setHospid(hospid);
        patiOutVisitSettle.setSerialnumber(serialNumber);
        //结账编码 0未结账
        patiOutVisitSettle.setFootid("0");
        patiOutVisitSettleMapper.outsettle_add(patiOutVisitSettle);
        return settleID;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String insertPatiOutVisitSettleOut(DictEmployee employee, PatiOutVisitSettleVo patiOutVisitSettleVo) throws Exception {
        //通过Serialnumber 查询结算表
        PatiOutVisitSettle povs =  patiOutVisitSettleMapper.outsettle_select(patiOutVisitSettleVo.getSerialnumber());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date today= new Date();
        //结算号
        String settleID = WorkIdUtil.nextId();
        povs.setSettleid(settleID);
        //结算日期
        Date settleDate = simpleDateFormat.parse(simpleDateFormat.format(today));
        povs.setSettledate(settleDate);
        //结算金额
        povs.setSettleamount(povs.getSettleamount().multiply(new BigDecimal(-1)));
        //病人负担金额
        povs.setPatiamount(povs.getPatiamount().multiply(new BigDecimal(-1)));
        //操作员
        povs.setEmpid(employee.getEMPID());
        //结账编码
        String footid = "0";
        povs.setCreatetime(settleDate);
        povs.setUpdatetime(settleDate);

        return settleID;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePatiOutVisitSettle(DictEmployee employee, PatiOutVisitSettleVo patiOutVisitSettleVo) throws Exception {
        PatiOutVisitSettle patiOutVisitSettle = BeanUtil.copyBean(patiOutVisitSettleVo,PatiOutVisitSettle.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date today= new Date();
        Date updatetime = simpleDateFormat.parse(simpleDateFormat.format(today));
        patiOutVisitSettle.setUpdatetime(updatetime);
        patiOutVisitSettle.setCancelempid(employee.getEMPID());
        patiOutVisitSettle.setCancelreason("退号作废");
        patiOutVisitSettleMapper.outsettle_update(patiOutVisitSettle);
    }
}
